<template>
    <div class="main">
        <h3 style="text-align: center;">智慧高尔夫球场</h3> 
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="form.username" name="username" label="用户名" placeholder="用户名"
                    :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="form.password" type="password" name="password" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
                <van-button round block type="primary" @click="toReg" style="margin-top: 30px;">
                    注册
                </van-button>
            </div>
        </van-form>
    </div>

</template>

<script setup>
import { showToast } from 'vant';
import {login} from '../api/index'
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter()
const form =reactive({
    username:'',
    password:''
})
const onSubmit = (values) => {
    login(form).then(res=>{
      showToast({
        message:res.message
      })
      if(res.code === 200){
        localStorage.setItem('token',res.token)
        router.push('/index')
      }
    })
};

const toReg = () => {
    router.push('/reg')
}

</script>

<style scoped>

.main{
    margin-top: 150px;
}
</style>